package org.black.phoneUtility.widget;

import org.black.phoneUtility.R;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class ContactGroupCursorAdapter extends CursorAdapter {

	private static final String LOG_TAG = ContactGroupCursorAdapter.class
			.getName();

	final static class ViewHolder {
		TextView groupNameTextView;
		TextView accountNameTextView;
		TextView groupCountTextView;
	}

	LayoutInflater layoutInflater = null;

	public ContactGroupCursorAdapter(Context context, Cursor cursor, int flag) {
		super(context, cursor, flag);
		this.layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();
		viewHolder.groupNameTextView.setText(cursor.getString(cursor
				.getColumnIndex(ContactsContract.Groups.TITLE)));
		viewHolder.accountNameTextView.setText(cursor.getString(cursor
				.getColumnIndex(ContactsContract.Groups.ACCOUNT_NAME)));
		int count = cursor.getInt(cursor
				.getColumnIndex(ContactsContract.Groups.SUMMARY_COUNT));
		Log.i(LOG_TAG, count + " people have in this group");
		viewHolder.groupCountTextView.setText(context.getResources()
				.getQuantityString(R.plurals.contact_group_people_in_group,
						count, count));
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View result = this.layoutInflater.inflate(
				R.layout.contact_group_list_row, parent, false);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.groupNameTextView = (TextView) result
				.findViewById(R.id.contactGroupNameTextView);
		viewHolder.groupCountTextView = (TextView) result
				.findViewById(R.id.contactGroupPeopleCountTextView);
		viewHolder.accountNameTextView = (TextView) result
				.findViewById(R.id.contactGroupAccountNameTextView);
		result.setTag(viewHolder);
		return result;
	}

}
